package com.hst.capacity.facade.rest;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.hst.capacity.application.biz.AgentBiz;
import com.hst.capacity.domain.exception.BusinessException;
import com.hst.capacity.domain.model.request.agent.AgentSaveParam;
import com.hst.capacity.domain.model.request.agent.AgentSearchParam;
import com.hst.capacity.domain.model.request.agent.AgentStatusParam;
import com.hst.capacity.domain.model.request.franchisee.FranchiseeParam;
import com.hst.capacity.domain.model.response.DefaultVO;
import com.hst.capacity.domain.model.response.agent.AgentCommisionSaveParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.Map;

import static com.hst.capacity.domain.model.response.DefaultVO.success;

/**
 *  服务商模块Controller
 *
 * */
@RequestMapping("/agent")
@RestController
public class AgentController {
    @Autowired
    private AgentBiz agentBiz;

    /**
     * 服务商检索查询
     * @param param
     * @return
     */
    @PostMapping("/search")
    private DefaultVO search(@RequestBody @Valid AgentSearchParam param){
        return success(agentBiz.search(param));
    }

    /**
     * 服务商保存/添加
     * @param param
     * @return
     */
    @PostMapping("/save")
    private DefaultVO save(@RequestBody @Valid AgentSaveParam param){
        return success(MapUtil.builder("idAgent",agentBiz.save(param)).build());
    }
    /**
     * 服务商编辑查询
     * @param param
     * @return
     */
    @PostMapping("/select")
    private DefaultVO select(@RequestBody Map<String,String> param){
        String idAgent = param.get("idAgent");
        if (StrUtil.isEmpty(idAgent))
            throw new BusinessException(4001,"服务商ID[idAgent]不能为空");
        return success(agentBiz.select(idAgent));
    }

    /**
     * 服务商开启
     * @param param
     * @return
     */
    @PostMapping("/status/open")
    private DefaultVO statusOpen(@RequestBody @Valid AgentStatusParam param){
        return success(agentBiz.statusOpen(param));
    }

    /**
     * 服务商冻结
     * @param param
     * @return
     */
    @PostMapping("/status/freeze")
    private DefaultVO statusFreeze(@RequestBody @Valid AgentStatusParam param){
        return success(agentBiz.statusFreeze(param));
    }

    /**
     * 服务商司机数量统计
     * @param param
     * @return
     */
    @PostMapping("/driver/count")
    private DefaultVO driverAgentCount(@RequestBody Map<String,String> param){
        String idAgent = param.get("idAgent");
        if (StrUtil.isEmpty(idAgent))
            throw new BusinessException(4001,"服务商ID[idAgent]不能为空");
        return success(agentBiz.driverAgentCount(idAgent));
    }
    /**
     * 获取运营商下的服务商
     * @param param
     * @return
     */
    @PostMapping("/by/franchisee/list")
    private DefaultVO byFranchiseeList(@RequestBody Map<String,String> param){
        String idFranchisee = param.get("idFranchisee");
        if (StrUtil.isEmpty(idFranchisee))
            throw new BusinessException(4001,"运营商ID[idFranchisee]不能为空");
        return success(agentBiz.byFranchiseeList(idFranchisee));
    }
    /**
     * 服务商分成比例展示
     * @param param
     * @return
     */
    @PostMapping("/commision/select")
    private DefaultVO commisionSelect(@RequestBody Map<String,String> param){
        String idAgent = param.get("idAgent");
        if (StrUtil.isEmpty(idAgent))
            throw new BusinessException(4001,"服务商ID[idAgent]不能为空");
        return success(agentBiz.commisionSelect(idAgent));
    }

    /**
     * 服务商分成比例保存
     * @param param
     * @return
     */
    @PostMapping("/commision/save")
    private DefaultVO commisionSave(@RequestBody@Valid AgentCommisionSaveParam param){
        return success(MapUtil.builder("idAgentCommision",agentBiz.commisionSave(param)).build());
    }

    /**
     * 服务商分成比例编辑查询
     * @param param
     * @return
     */
    @PostMapping("/commision/save/select")
    private DefaultVO commisionSaveSelect(@RequestBody Map<String,String> param){
        String idAgentCommision = param.get("idAgentCommision");
        if (StrUtil.isEmpty(idAgentCommision))
            throw new BusinessException(4001,"分成比例ID[idAgentCommision]不能为空");
        return success(agentBiz.commisionSaveSelect(idAgentCommision));
    }

    /**
     * 服务商数量统计
     * @param param
     * @return
     */
    @PostMapping("/refresh/number/count")
    private DefaultVO refreshAgentNumber(@RequestBody Map<String,String> param){
        String idAgent = param.get("idAgent");
        if (StrUtil.isEmpty(idAgent))
            throw new BusinessException(4001,"服务商ID[idAgent]不能为空");
        return success(agentBiz.refreshAgentNumber(idAgent));
    }

}
